{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finite Sequences\n",
    "======"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = ['{\"name\": \"Alice\", \"value\": 1}',\n",
    "        '{\"name\": \"Bob\", \"value\": 2}',\n",
    "        '{\"name\": \"Alice\", \"value\": 3}',\n",
    "        '{\"name\": \"Alice\", \"value\": 4}',\n",
    "        '{\"name\": \"Charlie\", \"value\": 5}',\n",
    "        '{\"name\": \"Bob\", \"value\": 6}',\n",
    "        '{\"name\": \"Alice\", \"value\": 7}']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': 'Alice', 'value': 1},\n",
       " {'name': 'Bob', 'value': 2},\n",
       " {'name': 'Alice', 'value': 3},\n",
       " {'name': 'Alice', 'value': 4},\n",
       " {'name': 'Charlie', 'value': 5},\n",
       " {'name': 'Bob', 'value': 6},\n",
       " {'name': 'Alice', 'value': 7}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq = list(map(json.loads, data))\n",
    "seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import toolz\n",
    "seq = list(toolz.pluck('value', seq))\n",
    "seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(seq)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Infinite Sequences\n",
    "=========="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def infinite_data():\n",
    "    for x in data:\n",
    "        yield x\n",
    "        \n",
    "    # Here we stop, but we could keep going forever...\n",
    "    raise StopIteration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from operator import add\n",
    "seq = infinite_data()\n",
    "seq = map(json.loads, seq)\n",
    "seq = toolz.pluck('value', seq)\n",
    "seq = toolz.accumulate(add, seq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "6\n",
      "10\n",
      "15\n",
      "21\n",
      "28\n"
     ]
    }
   ],
   "source": [
    "for item in seq:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Branching Sequences\n",
    "\n",
    "Sometimes we want to do multiple things with an infinite sequence.  This is where the Python iterator abstraction starts to feel awkward."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "import logging\n",
    "from collections import deque\n",
    "\n",
    "seq = infinite_data()\n",
    "seq = map(json.loads, data)\n",
    "\n",
    "seq1, seq2 = itertools.tee(seq, 2)\n",
    "\n",
    "seq1 = toolz.pluck('value', seq1)  # what we did before\n",
    "seq1 = toolz.accumulate(add, seq1)\n",
    "\n",
    "last_three = deque(maxlen=3)\n",
    "seq2 = map(last_three.append, seq2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "deque([{'name': 'Alice', 'value': 1}], maxlen=3)\n",
      "3\n",
      "deque([{'name': 'Alice', 'value': 1}, {'name': 'Bob', 'value': 2}], maxlen=3)\n",
      "6\n",
      "deque([{'name': 'Alice', 'value': 1}, {'name': 'Bob', 'value': 2}, {'name': 'Alice', 'value': 3}], maxlen=3)\n",
      "10\n",
      "deque([{'name': 'Bob', 'value': 2}, {'name': 'Alice', 'value': 3}, {'name': 'Alice', 'value': 4}], maxlen=3)\n",
      "15\n",
      "deque([{'name': 'Alice', 'value': 3}, {'name': 'Alice', 'value': 4}, {'name': 'Charlie', 'value': 5}], maxlen=3)\n",
      "21\n",
      "deque([{'name': 'Alice', 'value': 4}, {'name': 'Charlie', 'value': 5}, {'name': 'Bob', 'value': 6}], maxlen=3)\n",
      "28\n",
      "deque([{'name': 'Charlie', 'value': 5}, {'name': 'Bob', 'value': 6}, {'name': 'Alice', 'value': 7}], maxlen=3)\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    try:\n",
    "        item = next(seq1)\n",
    "        print(item)\n",
    "    \n",
    "        next(seq2)\n",
    "        print(last_three)\n",
    "        \n",
    "    except StopIteration:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Also want\n",
    "\n",
    "-  Handle multiple incoming streams with joins\n",
    "-  Perform time-window operations like \"group by 50 ms\" or \"slow down input stream, I'm swamped\"\n",
    "-  ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Streamz\n",
    "=====\n",
    "\n",
    "Same applications, just a different way of thinking about controlling data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from streamz import Stream"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "L = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<streamz.core.Sink at 0x7f25000b9b00>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Simple linear stream\n",
    "source = Stream()\n",
    "stream = (source.map(json.loads)\n",
    "                .map(lambda x: x['value'])\n",
    "                .scan(add))\n",
    "\n",
    "# Two actions whenever a value comes through\n",
    "stream.sink(print)\n",
    "stream.sink(L.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "6\n",
      "10\n",
      "15\n",
      "21\n",
      "28\n"
     ]
    }
   ],
   "source": [
    "for line in data:\n",
    "    source.emit(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 6, 10, 15, 21, 28]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "128\n"
     ]
    }
   ],
   "source": [
    "source.emit('{\"name\": \"Charlie\", \"value\": 100}');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 6, 10, 15, 21, 28, 128]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy to add on new components"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<streamz.core.Sink at 0x7f25000b9cf8>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stream.sliding_window(2).sink(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "129\n",
      "131\n",
      "(129, 131)\n",
      "134\n",
      "(131, 134)\n",
      "138\n",
      "(134, 138)\n",
      "143\n",
      "(138, 143)\n",
      "149\n",
      "(143, 149)\n",
      "156\n",
      "(149, 156)\n"
     ]
    }
   ],
   "source": [
    "for line in data:\n",
    "    source.emit(line)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
